package com.mall.order.mq.promo;

import com.alibaba.fastjson.JSON;
import com.mall.order.OrderPromoService;
import com.mall.order.dto.CreateSeckillOrderRequest;
import com.mall.order.dto.CreateSeckillOrderResponse;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author XuBang
 * @TODO: 事务消息的消费者：事务消息的提供者producer，发送消息成功（扣减库存成功）---->RocketMQ，将成功消息传递到此处：执行秒杀下单
 * @date 2021/11/7$
 * @trainOfthought:
 */
@Component
public class CreatePromoOrderConsumer {

    @Autowired
    OrderPromoService orderPromoService;


    DefaultMQPushConsumer consumer = null;

    //自定义init()方法&@PostConstruct--->spring容器可以管理：DefaultMQPushConsumer
    @PostConstruct
    public void init() {
        //1.定义消费者对象
        consumer = new DefaultMQPushConsumer("transaction_message_consumer");

        //2.设置地址
        consumer.setNamesrvAddr("127.0.0.1:9876");

        //3.设置监听器
        consumer.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                //消费的业务逻辑
                String data = null;
                try {
                    MessageExt message = msgs.get(0);
                    byte[] body = message.getBody();
                    data = new String(body, 0, body.length, "utf-8");

                    //将获取到的data还原为最初producer传送过来的JavaBean对象
                    CreateSeckillOrderRequest request = JSON.parseObject(data, CreateSeckillOrderRequest.class);  //将获取的string data还原为CreateSeckillOrderRequest类型的JavaBean对象
                    CreateSeckillOrderResponse promoOrder = orderPromoService.createPromoOrder(request);   //秒杀下单

                    //根据返回结果判断，返回RECONSUME_LATER  || CONSUME_SUCCESS
//                    if(){
//
//                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        //4.订阅
        try {
            consumer.subscribe("", "*");//*：不进行过滤
        } catch (MQClientException e) {
            e.printStackTrace();
        }

        //5.启动
        try {
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }
}
